package com.controller;

import com.bean.dto.ProductDto;
import com.bean.vo.ChainTypeQueryVo;
import com.service.ChainTypeService;
import com.service.ProductSpecificationService;
import com.util.ProductDtoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("ct")
public class ChainTypeController {
    @Autowired
    ChainTypeService cts;
    @Autowired
    ProductSpecificationService pss;
    /*查询产品规格信息（全查，单查，模糊）*/
    @RequestMapping("query")
    @ResponseBody
    public ProductDto queryAll(ChainTypeQueryVo ctv,Integer start_code,Integer page_size){
        System.out.println(">>>>>>>>>>查询");
        System.out.println(ctv);
        System.out.println(start_code);
        System.out.println(page_size);
        start_code= (start_code-1)*page_size;
        ctv.setPage_size(page_size);
        ctv.setStart_code(start_code);
        List<ChainTypeQueryVo> list = cts.QueryAll(ctv);
        System.out.println(list);
        System.out.println("长度:"+list.size());
        if (list.size() != 0){
            int count = cts.count(ctv);
            return ProductDtoUtil.returnSuccess(list,count);
        }
        return ProductDtoUtil.returnFail("无结果");
    }
    /*保存产品规格信息*/
    @RequestMapping("save")
    @ResponseBody
    public ProductDto Add(ChainTypeQueryVo ctv){
        System.out.println(">>>>>>>>>>添加");
        System.out.println(ctv);
        Long i = pss.add(ctv);
        System.out.println("i："+i);
        ctv.getCt().setProduct_specificationId(i);
        int count = cts.add(ctv);
        System.out.println("count:"+count);
        if (count != 0){
            return ProductDtoUtil.returnSuccess("添加成功!");
        }
        return ProductDtoUtil.returnFail("添加失败");
    }
    /*修改产品规格信息*/
    @ResponseBody
    @RequestMapping("update")
    public ProductDto update(ChainTypeQueryVo ctv){
        System.out.println(">>>>>>>>>修改单体");
        System.out.println(ctv);
        Long i = pss.update(ctv);
        System.out.println("i:"+i);
        ctv.getCt().setProduct_specificationId(i);
        int count = cts.update(ctv);
        if (count!=0){
            return ProductDtoUtil.returnSuccess("修改成功！");
        }
        return ProductDtoUtil.returnFail("修改失败！");
    }
    /*删除产品规格信息（真删除）*/
    @ResponseBody
    @RequestMapping("delete")
    public ProductDto delete(ChainTypeQueryVo ctv){
        System.out.println(">>>>>>>>>>>>>>>删除（真删）");
        System.out.println(ctv);
        int count = cts.delete(ctv);
        if (count != 0){
            System.out.println("success!");
            return ProductDtoUtil.returnSuccess("删除成功!");
        }
        System.out.println("wrong!");
        return ProductDtoUtil.returnFail("删除失败！");
    }
    /*删除产品规格信息（假删除）*/
    @ResponseBody
    @RequestMapping("fakeDelete")
    public ProductDto fakeDelete(ChainTypeQueryVo ctv){
        System.out.println(">>>>>>>>>>>>>>>>>>删除（假）");
        System.out.println(ctv);
        int count = cts.fakeDelete(ctv);
        if (count != 0){
            System.out.println("success!");
            return ProductDtoUtil.returnSuccess("删除成功!");
        }
        System.out.println("wrong!");
        return ProductDtoUtil.returnFail("删除失败！");
    }
}
